package com.wf.course.web.controller.comm;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.utils.page.PageBean;
import com.wf.course.model.comm.Activity;
import com.wf.course.service.IActivityService;
import com.wf.course.web.Interceptor.LogInfo;

@Controller
@RequestMapping("activity")
public class ActivityController {
	
	@Autowired
	private IActivityService activityService;
	
	/**
	 * 获取优惠活动列表
	 * @return
	 */
	@LogInfo("获取优惠活动列表")
	@RequestMapping("activityList")
	public String activityList(
				@RequestParam(value="pgaeNo",required=false) Integer pageNo,
				@RequestParam(value="pageSize",required=false) Integer pageSize,
				@RequestParam(value="title",required=false) String title,
				Model model){
		Map<String, Object> map = new HashMap<>();
		map.put("title", title);
		
		PageBean<Activity> pageBean = new PageBean<Activity>(pageNo,pageSize,null,map);
		this.activityService.getActivityList(pageBean);
		pageBean.setUrl("activity/activityList");
		
		model.addAttribute("page",pageBean);
		return "comm/activityList";
	}
	
	/**
	 * 修改优惠活动信息页加载
	 * @return
	 */
	@LogInfo("修改优惠活动信息页加载")
	@RequestMapping("activityLoad/{id}")
	public String activityLoad(@PathVariable("id") Integer id,Model model){
		if (id!=null && id >0) {
			//修改
			Activity activity = this.activityService.getActivity(id);
			model.addAttribute("page",activity);
			model.addAttribute("type","upd");
		}else{
			model.addAttribute("ids",id);
			//新增
			model.addAttribute("type","add");
		}
		
		return "comm/activityEdit";
	}
	
	/**
	 * 修改优惠活动信息
	 * @param activity
	 */
	@LogInfo("修改优惠活动信息")
	@RequestMapping("activityEdit")
	public String activityEdit(@ModelAttribute Activity activity,HttpServletRequest request){
			if (activity.getId()==null) {
				this.activityService.saveActivity(activity);
			}else{
				this.activityService.updateActivity(activity);
			}
		return "redirect:/activity/activityList";
	}
	
	/**
	 * 删除一条优惠活动信息
	 * @param id
	 * @return
	 */
	@LogInfo("删除一条优惠活动信息")
	@RequestMapping("activityDelete/{id}")
	public String activityDelete(@PathVariable("id") Integer id){
		this.activityService.deleteActivity(id);
		
		return "redirect:/activity/activityList";
		
	}
}
